import { defineStore } from 'pinia'
import api from "../api";
import { ACTION, STATE } from "../utils/class";
import {Communication, getCom} from "../utils/Communication";
import { computed, ref } from "vue";
const ipcHandle = () => window.electron.ipcRenderer.send('setCodeHidden')

export const useAppStore = defineStore('app', () => {

    const input = ref('')
    const isAltDown = ref(true)
    const info = ref('等待就绪')
    const taskQueue = ref([])
    const taskLog = ref([])
    const running = ref(false)
    const loading = ref(false)
    const inputRef = ref(null)
    const isNoDrag = ref(false)
    const stateAI =  ref(false)
    const stateEX =  ref(false)
    const isCommand = ref(false)
    const addValue = ref('')
    const showModal = ref(false)
    const isReady = computed(() => stateAI.value && stateEX.value)
    const options = ref([
        {
            label: "点击一个目标",
            value: "open C:\\Users\\chen\\Desktop\\24年4月仓库表.xlsx",
            key: "open C:\\Users\\chen\\Desktop\\24年4月仓库表.xlsx",
        },
        {
            label: "输入文字",
            value: "ocr 货值,单价,数量,年月,进出仓"
        },
        {
            label: "按下按键",
            value: "table all"
        },
        // {
        //     label: "右侧新增一行，列标签为货物价值，公式为单价乘以数量01",
        //     value: "click 1000,1000"
        // },
        // {
        //     label: "右侧新增一行，列标签为货物价值，公式为单价乘以数量02",
        //     value: "cv 货物价值"
        // },
        // {
        //     label: "右侧新增一行，列标签为货物价值，公式为单价乘以数量03",
        //     value: "click 1000,1000"
        // },
        // {
        //     label: "右侧新增一行，列标签为货物价值，公式为单价乘以数量04",
        //     value: "cv =(H2*E2)"
        // },
        // {
        //     label: "将公式应用到整列",
        //     value: "apply "
        // },
        // {
        //     label: "右侧新增一行，列标签为年月01",
        //     value: "dclick 1860，450"
        // },
        // {
        //     label: "右侧新增一行，列标签为年月02",
        //     value: "cv 年月"
        // },
        // {
        //     label: "右侧新增一行，列标签为年月03",
        //     value: "dclick 1860，450"
        // },
        // {
        //     label: "右侧新增一行，列标签为年月04",
        //     value: "cv =left(#单号#2,6)"
        // },
        // {
        //     label: "将公式应用到整列",
        //     key: "drag 1860，450，1000，1000"
        // },
        // {
        //     label: "新建数据透视表01",
        //     key: "click 1860，450"
        // },
        // {
        //     label: "新建数据透视表02",
        //     key: "click 插入"
        // },
        // {
        //     label: "新建数据透视表03",
        //     key: "click 数据透视表"
        // },
        // {
        //     label: "拖动年月至行,拖动货值至值,拖动入库出库至列01",
        //     key: "drag 1860，450,1000，1000"
        // },
        // {
        //     label: "拖动年月至行,拖动货值至值,拖动入库出库至列02",
        //     key: "drag 1860，450,1000，1000"
        // },
        // {
        //     label: "拖动年月至行,拖动货值至值,拖动入库出库至列03",
        //     key: "drag 1860，450,1000，1000"
        // },
        // {
        //     label: "读取数据",
        //     key: "ocr all"
        // },
        // {
        //     label: "AI",
        //     key: "根据24年4月仓库表，计算进出仓货值，及同比情况。"
        // }
    ])

    function onEXMessage(data){
        console.log(data)
        const { state } = data
        if (state === STATE.DONE) {
            running.value = false
            info.value = "执行完毕:" + data.action+" " +data.target
            removeTaskQueue()
            // 处理任务队列
            EXTask()
        }
    }
    function onAIMessage(data){
        console.log(data)
        if (data.action !== ACTION.NULL) {
            addTaskQueue(data)
        }else {
            info.value = data.info
        }
    }

    function changeState(name ,state){
        if (name === 'Server'){
            stateAI.value = state
        }else {
            stateEX.value = state
        }
    }

    function connect (){
        api.connectAI()
        api.connectEX()
    }

    function addTaskQueue (task){
        taskQueue.value.push(task)
        EXTask()
    }

    function removeTaskQueue (){
        taskQueue.value.shift()
    }

    function EXTask () {
        // 当任务进行时，跳过
        if (stateEX.value === false){
            return
        }
        if (taskQueue.value.length === 0 ){
            loading.value = false
            setTimeout(() => {info.value = '准备就绪'},3000)

            return
        }
        if (running.value === true){
            return
        }
        // 当任务没在进行，且有任务时，开始执行第一个任务货物价值=(H2*E2)=left(A2,6)
        if (taskQueue.value.length > 0 && !running.value) {
            loading.value = true
            running.value = true
            info.value ="执行："+ taskQueue.value[0].info
            if ( taskLog.value.length === 0 || taskQueue.value[0]?.info !== taskLog.value[taskLog.value.length-1]?.info){
                taskLog.value.push(taskQueue.value[0])
            }
            api.sendEXMessage(taskQueue.value[0])
        }

    }
    function handleSelect (e){
        input.value = input.value.concat(e)
        isCommand.value =false
        inputRef.value?.focus();
        isNoDrag.value = false
    }
    function onInput (e){
        if (input.value === '、' ) input.value = '/'
        if(input.value === '/') {
            isCommand.value = true
            isNoDrag.value = true
        }else{
            isCommand.value = false
            isNoDrag.value = false
        }

    }
    function getShow(){
        return input.value === '/';
    }
    function sendMessage(){
        // 去除前后空格

        if (input.value.trim().length === 0) return

        if (input.value[0] === '/'){
            sendToEX()
        }else {
            sendToAI()
        }
    }

    function sendToEX(){
        loading.value = true
        let data = {}
        let last =  input.value.indexOf(' ')
        let action = input.value.slice(1,last)
        let target = input.value.slice(last+1)

        if (target.includes(',') || target.includes('，')) {
            // 先按逗号分割
            let splitByComma = target.split(',');
            // 再按全角逗号分割每个由逗号分割出来的子串
            target = splitByComma.map(part => part.split('，')).flat();
        }
        data = getCom(action,action,target,STATE.ING)
        info.value = "开始执行："+ data.action
        api.sendEXMessage(data)
    }
    function sendToAI(){
        api.sendAIMessage(getCom(input.value))
    }
    function showCodePage(){
        console.log("showCodePage")
        ipcHandle()
    }

    return { info ,taskLog,isAltDown,stateAI,stateEX,showModal,isReady,input,addValue,taskQueue,loading,isNoDrag,isCommand,options,inputRef,running,getShow,EXTask,sendMessage,connect,changeState,showCodePage,onAIMessage,handleSelect,onInput,onEXMessage }
})
